package com.wx.tgp.socket.demo.password;

import java.util.Date;
import java.util.Random;

/**
 * 单独开一个线程来计算
 */
public class ThreadPWD implements Runnable{

    private String[] date={"0","1","2","3","4","5","6","7","8","9",
            "A","B","C","D","E","F","G","H","I","J",
            "K","L","M","N","O","P","Q","R","S","T",
            "U","V","W","X","Y","Z"};
    private String threadName;
    public ThreadPWD(String name){
        this.threadName=name;
        System.out.println(this.threadName+"线程创建成功。。。。。");
    }

    // 存放结果，再次使用需要重新new
    private StringBuffer result = null;
    private Random random = new Random();
    @Override
    public void run() {
        int count = 0;
        while(count<=30000L){
            count++;
            // 计算第1位
            int size=random.nextInt(36);
            result.append(date[size]);
            // 计算第2位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第3位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第4位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第5位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第6位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第7位
            size = random.nextInt(36);
            result.append(date[size]);
            // 计算第8位
            size = random.nextInt(36);
            result.append(date[size]);
            System.out.println(threadName+"   ：  "+result.toString());
            synchronized (ResultItem.resultMap){
                String pwd = ResultItem.resultMap.get(result.toString());
                if(pwd==null){
                    ResultItem.resultMap.put(result.toString(),result.toString());
                    Integer size1 = Integer.parseInt(ResultItem.resultMap.get("size"));
                    ResultItem.resultMap.remove("size");
                    ResultItem.resultMap.put("size",(size1++).toString());
                }
            }

        }

    }
}
